package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.ChangeOrderAddressBean;
import com.ymatou.operate.trading.api.service.ChangeOrderAddressCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.UserTypeEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 修改收货人地址 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeOrderAddress {
	private static ChangeOrderAddressBean coabean;
	private static ChangeOrderAddressCall coacall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改收货人地址");
	}

	@Before
	public void caseUp() {

		coabean = new ChangeOrderAddressBean();
		coacall = new ChangeOrderAddressCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(coacall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_001() {
		Logger.start(true,"订单状态为待付款,买家修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			
			Logger.comment("前置数据准备结束");		
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(receievePerson, database.selectOrderByOderId2(orderId).get("sReceivePerson"),
					"验证修改后的收货人");
			Logger.verifyEquals(address, database.selectOrderByOderId2(orderId).get("sAddress"), "验证修改后的收货地址");
			Logger.verifyEquals(postCode, database.selectOrderByOderId2(orderId).get("sPostCode"), "验证修改后的邮编");
			Logger.verifyEquals(phone, database.selectOrderByOderId2(orderId).get("sPhone"), "验证修改后的手机");
			Logger.verifyEquals(telephone, database.selectOrderByOderId2(orderId).get("sTelephone"), "验证修改后的固定电话");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_002() {
		Logger.start(true,"订单状态为已付款，买家修改收货人地址成功");
		try {
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			
			Logger.comment("前置数据准备结束");		
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);		
			Logger.comment("前置数据准备结束");
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(receievePerson, database.selectOrderByOderId2(orderId).get("sReceivePerson"),
					"验证修改后的收货人");
			Logger.verifyEquals(address, database.selectOrderByOderId2(orderId).get("sAddress"), "验证修改后的收货地址");
			Logger.verifyEquals(postCode, database.selectOrderByOderId2(orderId).get("sPostCode"), "验证修改后的邮编");
			Logger.verifyEquals(phone, database.selectOrderByOderId2(orderId).get("sPhone"), "验证修改后的手机");
			Logger.verifyEquals(telephone, database.selectOrderByOderId2(orderId).get("sTelephone"), "验证修改后的固定电话");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_003() {
		Logger.start(true,"订单状态为已接单，买家修改收货人地址失败");
		try {
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			
			Logger.comment("前置数据准备结束");			
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.FAILED201, coacall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_14, coacall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_004() {
		Logger.start(true,"订单状态为待付款,卖家修改收货人地址失败");
		try {
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			
			Logger.comment("前置数据准备结束");		
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(sellerId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.FAILED201, coacall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_14, coacall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_005() {
		Logger.start(true,"订单状态为已接单，卖家修改收货人地址成功");
		try {
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			
			Logger.comment("前置数据准备结束");		
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);	
			Logger.comment("前置数据准备结束");
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(sellerId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(receievePerson, database.selectOrderByOderId2(orderId).get("sReceivePerson"),
					"验证修改后的收货人");
			Logger.verifyEquals(address, database.selectOrderByOderId2(orderId).get("sAddress"), "验证修改后的收货地址");
			Logger.verifyEquals(postCode, database.selectOrderByOderId2(orderId).get("sPostCode"), "验证修改后的邮编");
			Logger.verifyEquals(phone, database.selectOrderByOderId2(orderId).get("sPhone"), "验证修改后的手机");
			Logger.verifyEquals(telephone, database.selectOrderByOderId2(orderId).get("sTelephone"), "验证修改后的固定电话");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_006() {
		Logger.start(true,"订单状态为补款中，卖家修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			Map map = new OrderCallService().appSaveOrder();		
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 买家付款
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 卖家发起补款
			int price = 1+new Random().nextInt(50);
			BigDecimal payAmount = new BigDecimal(df.format(price));
			String postPayId = new OrderCallService().applyForPostPay(orderId, sellerId, payAmount);
			Logger.comment("前置数据准备结束");
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(sellerId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(receievePerson, database.selectOrderByOderId2(orderId).get("sReceivePerson"),
					"验证修改后的收货人");
			Logger.verifyEquals(address, database.selectOrderByOderId2(orderId).get("sAddress"), "验证修改后的收货地址");
			Logger.verifyEquals(postCode, database.selectOrderByOderId2(orderId).get("sPostCode"), "验证修改后的邮编");
			Logger.verifyEquals(phone, database.selectOrderByOderId2(orderId).get("sPhone"), "验证修改后的手机");
			Logger.verifyEquals(telephone, database.selectOrderByOderId2(orderId).get("sTelephone"), "验证修改后的固定电话");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_007() {
		Logger.start(true,"订单不存在，修改订单地址失败");
		try {
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			int orderId = 132314214;
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.FAILED201, coacall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11, coacall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderAddress_008() {
        Logger.start(false,"订单号为null,修改订单地址失败");
        try {
            // 设置参数
            String address = "南京路步行街";
            coabean.setAddress(address);
            String changeTime = YMTDateUtil.getSysTime();
            coabean.setChangeTime(changeTime);
            String email = TestDataManager.getEmail();
            coabean.setEmail(email);
            int orderId = 132314214;
            //coabean.setOrderId(orderId);
            String phone = "13200000005";
            coabean.setPhone(phone);
            String postCode = "986574";
            coabean.setPostCode(postCode);
            coabean.setQQ("100001");
            String receievePerson = "洋码头";
            coabean.setReceievePerson(receievePerson);
            String telephone = "021-51025140";
            coabean.setTelephone(telephone);
            coabean.setUserId(288484);
            coabean.setUserType(UserTypeEnum.Seller);
            // 发送请求
            coacall.setData(coabean);
            coacall.callService();
            Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
            Logger.verifyIsContains(ECode.ERROR400_MSG_13, coacall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category(P3.class)
    @TestCase
    public void Tc_ChangeOrderAddress_009() {
        Logger.start(false,"操作用户id为null,修改订单地址失败");
        try {
            // 设置参数
            String address = "南京路步行街";
            coabean.setAddress(address);
            String changeTime = YMTDateUtil.getSysTime();
            coabean.setChangeTime(changeTime);
            String email = TestDataManager.getEmail();
            coabean.setEmail(email);
            int orderId = 132314214;
            coabean.setOrderId(orderId);
            String phone = "13200000005";
            coabean.setPhone(phone);
            String postCode = "986574";
            coabean.setPostCode(postCode);
            coabean.setQQ("100001");
            String receievePerson = "洋码头";
            coabean.setReceievePerson(receievePerson);
            String telephone = "021-51025140";
            coabean.setTelephone(telephone);
            //coabean.setUserId(288484);
            coabean.setUserType(UserTypeEnum.Seller);
            // 发送请求
            coacall.setData(coabean);
            coacall.callService();
            Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
            Logger.verifyIsContains(ECode.ERROR400_MSG_12, coacall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	
	   @Test
	    @Category(P3.class)
	    @TestCase
	    public void Tc_ChangeOrderAddress_010() {
	        Logger.start(false,"收货人地址为null,修改订单地址失败");
	        try {
	            // 设置参数
	            String address = "南京路步行街";
	            //coabean.setAddress(address);
	            String changeTime = YMTDateUtil.getSysTime();
	            coabean.setChangeTime(changeTime);
	            String email = TestDataManager.getEmail();
	            coabean.setEmail(email);
	            int orderId = 132314214;
	            coabean.setOrderId(orderId);
	            String phone = "13200000005";
	            coabean.setPhone(phone);
	            String postCode = "986574";
	            coabean.setPostCode(postCode);
	            coabean.setQQ("100001");
	            String receievePerson = "洋码头";
	            coabean.setReceievePerson(receievePerson);
	            String telephone = "021-51025140";
	            coabean.setTelephone(telephone);
	            coabean.setUserId(288484);
	            coabean.setUserType(UserTypeEnum.Seller);
	            // 发送请求
	            coacall.setData(coabean);
	            coacall.callService();
	            Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
	            Logger.verifyIsContains(ECode.ERROR400_MSG_36, coacall.getMsg(), "验证Msg");
	        } catch (Exception e) {
	            Logger.fail(e);
	        }
	    }
	   
       @Test
       @Category(P3.class)
       @TestCase
       public void Tc_ChangeOrderAddress_011() {
           Logger.start(false,"收货人null,修改订单地址失败");
           try {
               // 设置参数
               String address = "南京路步行街";
               coabean.setAddress(address);
               String changeTime = YMTDateUtil.getSysTime();
               coabean.setChangeTime(changeTime);
               String email = TestDataManager.getEmail();
               coabean.setEmail(email);
               int orderId = 132314214;
               coabean.setOrderId(orderId);
               String phone = "13200000005";
               coabean.setPhone(phone);
               String postCode = "986574";
               coabean.setPostCode(postCode);
               coabean.setQQ("100001");
               String receievePerson = "洋码头";
               //coabean.setReceievePerson(receievePerson);
               String telephone = "021-51025140";
               coabean.setTelephone(telephone);
               coabean.setUserId(288484);
               coabean.setUserType(UserTypeEnum.Seller);
               // 发送请求
               coacall.setData(coabean);
               coacall.callService();
               Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
               Logger.verifyIsContains(ECode.ERROR400_MSG_37, coacall.getMsg(), "验证Msg");
           } catch (Exception e) {
               Logger.fail(e);
           }
       }
}